#include <iostream>
#include <string>

using namespace std;

string& UpConversion(string& str)
{
	int i = 0;
	int sz = str.size();
	while (i < sz)
	{
		if (str[i] >= 'A' && str[i] <= 'Z')
		{
			str[i] -= 'A' - 'a';
		}
		++i;
	}
	return str;
}

string& LowConversion(string& str)
{
	int i = 0;
	int sz = str.size();
	while (i < sz)
	{
		if (str[i] >= 'a' && str[i] <= 'z')
		{
			str[i] += 'A' - 'a';
		}
		++i;
	}
	return str;
}


int StringToInt(const string& str)
{
	int record = 0;
	int flag = 1;
	int i = 0;
	int sz = str.size();
	while (i < sz)
	{
		if (str[i] == '+' || str[i] == '-' || (str[i] >= '0' && str[i] <= '9'))
		{
			if (str[i] == '+')
			{
				flag = 1;
				++i;
			}
			else if (str[i] == '-')
			{
				flag = -1;
				++i;
			}
			while (str[i] >= '0' && str[i] <= '9')//&& i < sz)
			{
				record = (record * 10 + (str[i] - '0'));
				++i;
			}
			return flag * record;
		}
		++i;
	}
	return flag * record;
}

int StringToInt2(const string& str)
{
	int record = 0;
	int flag = 1;
	int sz = str.size();
	int i = sz - 1;

	while (i >= 0)
	{
		if (str[i] >= '0' && str[i] <= '9')
		{
			int crray = 1;
			while (i >= 0 && str[i] >= '0' && str[i] <= '9')
			{
				record += (str[i] - '0') * crray;
				crray *= 10;
				--i;
			}

			if (i >= 0 && str[i] == '+')
			{
				flag = 1;
				--i;
			}
			else if (i >= 0 && str[i] == '-')
			{
				flag = -1;
				--i;
			}
			return flag * record;
		}
		--i;
	}
	return flag * record;
}

int HexStringToInt(const string& s)
{
	int record = 0;
	int i = 0x00000000;
	int sz = s.size();
	string str = s;
	UpConversion(str);

	while (i < sz)
	{
		if (str[i] == '+' || str[i] == '-' || (str[i] >= '0' && str[i] <= '9') || (str[i] >= 'a' && str[i] <= 'f'))
		{
			while (str[i] >= '0' && str[i] <= '9' || (str[i] >= 'a' && str[i] <= 'f'))
			{
				if (str[i] >= '0' && str[i] <= '9')
				{
					record = (record * 16 + (str[i] - '0'));
					++i;
				}
				if (str[i] >= 'a' && str[i] <= 'f')
				{
					record = (record * 16 + ((str[i] - 'a') + 10));
					++i;
				}
			}
			return record;
		}
		++i;
	}
	return record;
}


int HexStringToInt2(const string& s)
{
	int record = 0;
	int sz = s.size();
	int i = sz - 1;
	string str = s;
	UpConversion(str);
	while (i >= 0)
	{
		if ((str[i] >= '0' && str[i] <= '9') || (str[i] >= 'a' && str[i] <= 'f'))
		{
			int crray = 1;
			while (i >= 0 && (str[i] >= '0' && str[i] <= '9') || (str[i] >= 'a' && str[i] <= 'f'))
			{
				if (str[i] >= '0' && str[i] <= '9')
				{
					record += (str[i] - '0') * crray;
					crray *= 16;
					--i;
				}
				if (str[i] >= 'a' && str[i] <= 'f')
				{
					record += ((str[i] - 'a') + 10) * crray;
					crray *= 16;
					--i;
				}
			}

			return record;
		}
		--i;
	}
	return  record;
}

void test1()
{
	string str1 = "ass + 12 as";
	string str2 = "ass +12 as";
	string str3 = "ass 12 as";
	string str4 = "ass -12 as";
	string str5 = "12 as";
	string str6 = "ass12 as";
	string str7 = "";
	string str8 = "12345";
	string str9 = "asdasd";


	int n = 0xFFFF;

	n = StringToInt(str1);
	cout << n << endl;

	n = StringToInt(str2);
	cout << n << endl;

	n = StringToInt(str3);
	cout << n << endl;

	n = StringToInt(str4);
	cout << n << endl;

	n = StringToInt(str5);
	cout << n << endl;

	n = StringToInt(str6);
	cout << n << endl;

	n = StringToInt(str7);
	cout << n << endl;

	n = StringToInt(str8);
	cout << n << endl;

	n = StringToInt(str9);
	cout << n << endl;


	n = StringToInt2(str1);
	cout << n << endl;

	n = StringToInt2(str2);
	cout << n << endl;

	n = StringToInt2(str3);
	cout << n << endl;

	n = StringToInt2(str4);
	cout << n << endl;

	n = StringToInt2(str5);
	cout << n << endl;

	n = StringToInt2(str6);
	cout << n << endl;

	n = StringToInt2(str7);
	cout << n << endl;

	n = StringToInt2(str8);
	cout << n << endl;

	n = StringToInt2(str9);
	cout << n << endl;
}

void test2()
{
	string str1 = "kkkkffff0000kkkk";
	string str2 = "kkkk00000000kkkk";
	string str3 = "kkkk0000ffffkkkk";
	string str4 = "kkkkffffffffkkkk";
	string str5 = "kkkk-ffffffffkkkk";
	string str6 = "kkkk+ffffffffkkkk";
	string str7 = "kkkkffffffff +1kkkk";
	string str8 = "kkkkffffffff -1kkkk";
	string str9 = "";


	int n = 0x0;

	n = HexStringToInt(str1);
	cout << hex << n << endl;

	n = HexStringToInt(str2);
	cout << hex << n << endl;

	n = HexStringToInt(str3);
	cout << hex << n << endl;

	n = HexStringToInt(str4);
	cout << hex << n << endl;

	n = HexStringToInt(str5);
	cout << hex << n << endl;

	n = HexStringToInt(str6);
	cout << hex << n << endl;

	n = HexStringToInt(str7);
	cout << hex << n << endl;

	n = HexStringToInt(str8);
	cout << hex << n << endl;

	n = HexStringToInt(str9);
	cout << hex << n << endl;


	cout << endl;
	cout << endl;


	n = HexStringToInt2(str1);
	cout << hex << n << endl;

	n = HexStringToInt2(str2);
	cout << hex << n << endl;

	n = HexStringToInt2(str3);
	cout << hex << n << endl;

	n = HexStringToInt2(str4);
	cout << hex << n << endl;

	n = HexStringToInt2(str5);
	cout << hex << n << endl;

	n = HexStringToInt2(str6);
	cout << hex << n << endl;

	n = HexStringToInt2(str7);
	cout << hex << n << endl;

	n = HexStringToInt2(str8);
	cout << hex << n << endl;

	n = HexStringToInt2(str9);
	cout << hex << n << endl;
}

void test3()
{
	string str1 = "kkkkffff0000kkkk";
	string str2 = "kkkk00000000kkkk";
	string str3 = "kkkk0000ffffkkkk";
	string str4 = "kkkkffffffffkkkk";
	string str5 = "kkkk-ffffffffkkkk";
	string str6 = "kkkk+ffffffffkkkk";
	string str7 = "kkkkffffffff +1kkkk";
	string str8 = "kkkkffffffff -1kkkk";
	string str9 = "";

	cout << LowConversion(str1) << endl;
	cout << LowConversion(str2) << endl;
	cout << LowConversion(str3) << endl;
	cout << LowConversion(str4) << endl;
	cout << LowConversion(str5) << endl;
	cout << LowConversion(str6) << endl;
	cout << LowConversion(str7) << endl;
	cout << LowConversion(str8) << endl;
	cout << LowConversion(str9) << endl;

	cout << UpConversion(str1) << endl;
	cout << UpConversion(str2) << endl;
	cout << UpConversion(str3) << endl;
	cout << UpConversion(str4) << endl;
	cout << UpConversion(str5) << endl;
	cout << UpConversion(str6) << endl;
	cout << UpConversion(str7) << endl;
	cout << UpConversion(str8) << endl;
	cout << UpConversion(str9) << endl;

}


int main()
{
	//test1();
	test2();
	//test3();
	return 0;
}